package company.wangyi_F;

import java.util.*;

public class Wang3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int[] array = new int[n];
            Map<Integer, Integer> dict = new HashMap<>();
            for (int j = 0; j < n; j++) {
                array[j] = sc.nextInt();
                dict.put(j, dict.getOrDefault(j, 0) + 1);
            }
            list.add(helper(dict, array));
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private static int helper(Map<Integer, Integer> dict, int[] array) {
        Arrays.sort(array);
        for (int i = array.length - 1; i >= 0; i--) {
            dict.put(i, dict.get(i) - 1);
            dict.put(i, dict.get(i) + 1);
        }
        int result = 0;
        for (Map.Entry<Integer, Integer> e : dict.entrySet()) {
            result += e.getKey() * e.getValue();
        }
        return result;
    }

}
